<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Validator;

class formValidate
{
    use \App\Traits\SessionTrait;
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $type = '')
    {
        $type = $request->input('form_type') ? $request->input('form_type') : $type;
        switch ($type) {
            case 'tech_device':
                $this->_techDeviceValidate($request);
                break;
            case 'worker':
                $this->_workerValidate($request);
                break;
            case 'boater':
                $this->_boaterValidate($request);
                break;
            case 'logined':
                if (!$this->sessionReturnCompanyId()) {
                    return redirect('/login');
                }
                break;
            case 'login':
                $this->_loginValidate($request);
                break;
            case 'sendSms':
                $this->_sendSms($request);
                break;
            default:
                # code...
                break;
        }
        return $next($request);
    }

    private function _loginValidate($request)
    {
        $rules = [
            'phone' => 'required',
            'pwd' => 'required',
            // 'captcha' => 'required|captcha',
        ];
        $messages = [
            'phone.required' => '手机号码不能为空',
            'pwd.required' => '密码不能为空',
            'captcha.captcha' => '验证码错误',
            'captcha.required' => '验证码必须',
        ];
        $validator = Validator::make($request->all(), $rules, $messages); //自定义错误信息
        if ($validator->fails()) {
            $this->_validateErrors($validator, -1);
        }
    }

    private function _sendSms($request)
    {
        $rules = [
            'phone' => 'required',
            // 'img_code' => 'required|captcha',
        ];
        $messages = [
            'phone.required' => '手机号码不能为空',
            // 'img_code.captcha' => '验证码错误',
            // 'img_code.required' => '验证码必须',
        ];
        $validator = Validator::make($request->all(), $rules, $messages); //自定义错误信息
        if ($validator->fails()) {
            $this->_validateErrors($validator, -1);
        }
    }

    private function _techDeviceValidate($request)
    {
        $rules = [
            'company_name' => 'required|max:100',
            'company_desc' => 'required|max:500',
            'user_name' => 'required|max:5',
            'user_phone' => 'required|digits:11',
        ];

        $messages = [
            'company_name.required' => '企业名称不能为空',
            'company_name.max' => '企业名称最长100字',
            'company_desc.required' => '企业概况不能为空',
            'company_desc.max' => '企业概况最长500字符',
            'user_name.required' => '联系人名称必须',
            'user_name.max' => '联系人名称最长5字符',
            'user_phone.required' => '联系人电话必须',
            'user_phone.digits' => '联系电话最长11字符',
        ];

        $validator = Validator::make($request->all(), $rules, $messages); //自定义错误信息
        if ($validator->fails()) {
            $this->_validateErrors($validator, -1);
        }
    }

    private function _workerValidate($request)
    {
        $rules = [
            'company_name' => 'required|max:100',
            'company_desc' => 'required|max:500',
            'user_name' => 'required|max:5',
            'user_phone' => 'required|digits:11',
            //'captcha' => 'required|captcha',
            'tech_device_price' => 'required|max:20',
            'fix_per_time' => 'required|max:20',
            'tech_device_fix_years' => 'required|max:20',
            'tech_device_case' => 'required|max:2000',
        ];
        $messages = [
            'company_name.required' => '企业名称不能为空',
            'company_name.max' => '最长100字',
            'company_desc.required' => '企业概况不能为空',
            'company_desc.max' => '最长500字符',
            'user_name.required' => '联系人名称必须',
            'user_name.max' => '最长5字符',
            'user_phone.required' => '联系人电话必须',
            'user_phone.digits' => '最长11字符',
            //'captcha.required' => '验证码必填',
            //'captcha.captcha' => '验证码错误',
            'tech_device_price.required' => '安装单价不能为空',
            'tech_device_price.max' => '最长20字符',
            'fix_per_time.required' => '安装工时不能为空',
            'fix_per_time.max' => '最长20字符',
            'tech_device_fix_years.required' => '保修期限不能为空',
            'tech_device_fix_years.max' => '最长20字符',
            'tech_device_case.required' => '典型案例不能为空',
            'tech_device_case.max' => '最长2000字符',
        ];
        $validator = Validator::make($request->all(), $rules, $messages); //自定义错误信息

        if ($validator->fails()) {
            $this->_validateErrors($validator, -1);
        }
    }

    private function _boaterValidate($request)
    {
        $rules = [
            'company_name' => 'required|max:100',
            'company_desc' => 'required|max:500',
            'user_name' => 'required|max:5',
            'user_phone' => 'required|digits:11',
            //'captcha' => 'required|captcha',
        ];

        $messages = [
            'company_name.required' => '企业名称不能为空',
            'company_name.max' => '最长100字',
            'company_desc.required' => '企业概况不能为空',
            'company_desc.max' => '最长500字符',
            'user_name.required' => '联系人名称必须',
            'user_name.max' => '最长5字符',
            'user_phone.required' => '联系人电话必须',
            'user_phone.digits' => '最长11字符',
            //'captcha.required' => '验证码必填',
            //'captcha.captcha' => '验证码错误',
        ];

        $validator = Validator::make($request->all(), $rules, $messages); //自定义错误信息

        if ($validator->fails()) {
            $this->_validateErrors($validator, -1);
        }
    }

    private function _validateErrors($validator, $errorId)
    {
        $errors = (array) $validator->errors();
        foreach ($errors as $key => $value) {
            foreach ($value as $key2 => $value2) {
                $errorInfo = $value2[0];
                break;
            }
            break;
        }
        $jsonData = [
            'result' => $errorId,
            'msg' => $errorInfo,
        ];
        echo json_encode($jsonData);
        exit;
    }
}
